package com.yjt.admin.system.controller.org;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.entity.Org;
import com.yjt.admin.system.form.OrgForm;
import com.yjt.admin.system.form.org.DeptOrgForm;
import com.yjt.admin.system.param.org.DeptOrgParam;
import com.yjt.admin.system.param.org.OrgParam;
import com.yjt.admin.system.service.OrgService;
import com.yjt.admin.system.vo.org.DeptOrgVO;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.utils.CopyObject;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-09
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/dept-org")
@Api(tags = "部门管理")
public class DeptOrgController {

    @Autowired
    private OrgService orgService;

    @PostMapping("/add")
    @ApiOperation(value = "增加部门信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增部门信息")
    public CommonResult addDeptOrg(@RequestBody @Validated DeptOrgForm deptOrgForm) {
        OrgForm orgForm = new OrgForm();
        BeanUtils.copyProperties(deptOrgForm, orgForm);
        orgForm.setOrgType(SysCons.ORG_TYPE[3]);
        orgService.addOrg(orgForm);
        return CommonResult.success("部门新增成功");
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改部门信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改部门信息")
    public CommonResult updateDeptOrg(@RequestBody @Validated DeptOrgForm deptOrgForm) {
        OrgForm orgForm = new OrgForm();
        BeanUtils.copyProperties(deptOrgForm, orgForm);
        orgForm.setOrgType(SysCons.ORG_TYPE[3]);
        orgService.updateOrg(orgForm);
        return CommonResult.success("部门修改成功");
    }


    @GetMapping("/{orgId}")
    @ApiOperation(value = "查询部门信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "orgId", value = "部门id")
    public CommonResult<DeptOrgVO> getOneDeptOrg(@PathVariable String orgId) {
        OrgVO orgVO = orgService.getOneOrg(orgId);
        DeptOrgVO deptOrgVO = new DeptOrgVO();
        BeanUtils.copyProperties(orgVO, deptOrgVO);
        return CommonResult.success(deptOrgVO, "部门信息查询成功");
    }


    @SneakyThrows
    @PostMapping("/list-page")
    @ApiOperation(value = "查询部门分页信息", notes = "返回JSONObject")
    public CommonResult<IPage<DeptOrgVO>> getPageDeptOrg(@RequestBody DeptOrgParam deptOrgParam) {
        //复制查询参数
        OrgParam orgParam = new OrgParam();
        BeanUtils.copyProperties(deptOrgParam, orgParam);
        IPage<OrgVO> page = orgService.getPageOrg(orgParam);
        //构造返回参数
        List<OrgVO> orgListVO = page.getRecords();
        List<DeptOrgVO> deptOrgVOList = CopyObject.copyList(orgListVO, DeptOrgVO.class);
        IPage<DeptOrgVO> deptOrgVOPage = new Page();
        BeanUtils.copyProperties(page, deptOrgVOPage);
        deptOrgVOPage.setRecords(deptOrgVOList);
        return CommonResult.success(deptOrgVOPage, "查询部门分页信息成功");
    }

    @PutMapping("/forbid")
    @ApiOperation(value = "禁用部门", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.MOD, operDesc = "禁用部门")
    @ApiImplicitParam(name = "orgId", value = "部门id", dataType = "string")
    public CommonResult stopDeptOrg(String orgId) {
        //构造参数
        LambdaUpdateWrapper<Org> lambdaUpdateWrapper = Wrappers.<Org>lambdaUpdate()
                .set(Org::getIsForbid, 0)
                .eq(Org::getOrgId, orgId);
        orgService.update(lambdaUpdateWrapper);
        return CommonResult.success("部门禁用成功");
    }

}
